package com.shop.service.mqclient.reciver;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.shop.service.module.service.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Wenjl
 */
@Component
@RabbitListener(queues = "teamTimeOutQueue")
@Slf4j
public class TeamDeadReciver {
    @Autowired
    private TeamService teamService;

    @RabbitHandler
    public void process(JSONObject j, Channel channel, Message message) {
        //使用JSObject 接受消息内容，假设获得id在json对象中名为id
        log.info("触发死信队列");
        Long id = (Long) j.get("id");
        teamService.makeActivityTimeout(id);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
